<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/class.phpmailer.php'; 
require_once 'CMS/class.smtp.php'; 
require_once 'CMS/class.pop3.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php'; //include thu vien language

//Người tạo : Nguyễn Nghĩa Hưng - Team 3
//Module 	: Login
//Chức năng	: log in và log out

class Login_IndexController {

	public function init(){
		parent::init();
	
	}

// action login
	public function indexAction()
	{
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/login');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
		$this->view->textUser 		= $language->get('text_user');
		$this->view->textPass 		= $language->get('text_password');
		$this->view->textLogin 		= $language->get('text_login');
		$this->view->textForget 	= $language->get('text_forget');
		$this->view->textRegister 	= $language->get('text_register');
		$this->view->textWelcome 	= $language->get('text_welcome');
		$this->view->textLogout 	= $language->get('text_logout');
		$this->view->textEdit 		= $language->get('text_edit');
		$this->view->textTitle 		= $language->get('text_title');
		$this->view->textOrder 		= $language->get('text_order');
		$this->view->textSave 		= $language->get('text_save');
						
		$this->view->warningUser 	= $language->get('warningUser');
		$this->view->warningPass 	= $language->get('warningPass');				
		/*========== End Text ==============*/
				
		//Start------Check Input Parameter
		$path = APPLICATION_PATH."/widgets/login/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		//End--------Check Input Parameter
		
		$session = new Zend_Session_Namespace('login.auth');
		
        $errorMessage = "";
        try{
			if(isset($_POST["userID"])&&isset($_POST["userPass"]))
			{
				//start----nhận userId và pass từ form
				$user=$_POST["userID"];	
				$pass=md5($_POST["userPass"]);
				//end----nhận userId và pass từ form
				
				//start----truy suất csdl kiểm tra
				$tbTemp = new Login_Model_Users();
				$return=$tbTemp->checkLogin($user,$pass);
				//end ----truy suất csdl kiểm tra
				
				
				if(count($return)>0)
				{
					$userInfo=$return[0];
					if(isset($session->id_user)){}
					else
					{	
						
						$session->id_user = $userInfo['user_id'];
						$session->name_user = $userInfo['user_name'];	
					}	
				}
				else
				{ $errorMessage=$language->get('warning');}
			}
		}
		catch (Exception $e)
		{ 
			$errorMessage = $e->getMessage();
		}
		
		/************ Start check user login *********/
		if(isset($session->id_user))
		{
			$this->view->login = "1";
			$this->view->user_name = $session->name_user;
			/************ Start check permission *********/
			$user = new User();
			$user->init();
			$hasmodify = $user->modifiy_widget($xmlconfig->name);
			$this->view->permission = $hasmodify;
			/************ End check user permission *********/
		}
		else 
		{
			
			$this->view->login = "0";
		}
		/************ End check user login *********/
		
		//truyền thông báo lỗi
        $this->view->errorMessage = $errorMessage;
        
        //truyen title
        $titleXML = 'title'.$name['name'];
        if(isset($xmlconfig->$titleXML))
        	$this->view->title = $xmlconfig->$titleXML;
        else
        	$this->view->title = $xmlconfig->titleDefault;
	}
	
	
	
}